package com.masterclient.app.modules.purchase.widget;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/9/24.
 */
public class PurchaseCartNumDialog extends Dialog {

    @BindView(R.id.dialog_purchase_num_edit)
    EditText dialogPurchaseNumEdit;
    @BindView(R.id.dialog_purchase_num_cancel)
    TextView dialogPurchaseNumCancel;
    @BindView(R.id.dialog_purchase_num_confirm)
    TextView dialogPurchaseNumConfirm;

    private long maxNum;
    private String currentNum;
    private Activity context;

    public PurchaseCartNumDialog(@NonNull Activity context, String currentNum,long maxNum, CompleteClickListener completeClickListener) {
        super(context,R.style.MyDialog);
        this.context = context;
        this.maxNum = maxNum;
        this.completeClickListener = completeClickListener;
        this.currentNum = currentNum;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_purchase_cart_num);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(true);

        dialogPurchaseNumEdit.setText(currentNum);
        dialogPurchaseNumEdit.setSelection(currentNum.length());

        dialogPurchaseNumCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        dialogPurchaseNumConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                completeNum();
            }
        });



        dialogPurchaseNumEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE){
                    completeNum();
                    return true;
                }
                return false;
            }
        });

        dialogPurchaseNumEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s.toString()) && Long.parseLong(s.toString())>maxNum){
                    dialogPurchaseNumEdit.setText(String.valueOf(maxNum));
                    dialogPurchaseNumEdit.setSelection(String.valueOf(maxNum).length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void completeNum(){
        String str = dialogPurchaseNumEdit.getText().toString();
        if (TextUtils.isEmpty(str) || Long.parseLong(str)<1){
            return;
        }
        completeClickListener.onCompleteClick(Long.parseLong(str));
        dismiss();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        window.setAttributes(windowParams);
    }

    private CompleteClickListener completeClickListener;

    public interface CompleteClickListener{
        void onCompleteClick(long num);
    }

}
